<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="活动名称" prop="title">
        <el-input v-model="dataForm.title" placeholder="活动名称"></el-input>
      </el-form-item>
      <el-form-item label="活动图片" prop="images">
        <single-upload v-model="dataForm.images"></single-upload>
      </el-form-item>
      <el-form-item label="描述" prop="descs">
        <el-upload
          id="upimg"
          v-show="false"
          class="upload-demo"
          :action="imgUploadUrl"
          :on-success="handleSuccess"
          multiple
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>

        <quill-editor v-model="dataForm.descs" :options="editorOption" ref="QuillEditor"></quill-editor>
      </el-form-item>
      <el-form-item label="活动开始时间" prop="activeTime">
        <el-input v-model="dataForm.activeTime" placeholder="活动开始时间"></el-input>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="deleteTime">
        <el-input v-model="dataForm.deleteTime" placeholder="活动结束时间"></el-input>
      </el-form-item>
      <el-form-item label="一等奖" prop="firstPrize">
        <el-input v-model="dataForm.firstPrize" placeholder="一等奖"></el-input>
      </el-form-item>
      <el-form-item label="二等奖" prop="secondPrize">
        <el-input v-model="dataForm.secondPrize" placeholder="二等奖"></el-input>
      </el-form-item>
      <el-form-item label="三等奖" prop="thiredPrize">
        <el-input v-model="dataForm.thiredPrize" placeholder="三等奖"></el-input>
      </el-form-item>
      <el-form-item label="一等奖人数" prop="firstPeople">
        <el-input v-model="dataForm.firstPeople" placeholder="一等奖人数"></el-input>
      </el-form-item>
      <el-form-item label="二等奖人数" prop="secondPeople">
        <el-input v-model="dataForm.secondPeople" placeholder="二等奖人数"></el-input>
      </el-form-item>
      <el-form-item label="三等奖人数" prop="thirdPoeple">
        <el-input v-model="dataForm.thirdPoeple" placeholder="三等奖人数"></el-input>
      </el-form-item>
      <el-form-item label="参与奖" prop="participationAward">
        <el-input v-model="dataForm.participationAward" placeholder="参与奖"></el-input>
      </el-form-item>
      <el-form-item label="参与奖个数" prop="participationPeople">
        <el-input v-model="dataForm.participationPeople" placeholder="参与奖个数"></el-input>
      </el-form-item>

      <el-form-item label="是否可见" prop="status">
        <el-input v-model="dataForm.status" placeholder="是否可见"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "../../../components/upload/singleUpload";

// 富文本编辑器工具栏配置，因为涉及到富文本编辑器以外的组件调用，所以在这里我们需要重新自定义一下富文本编辑器的工具栏
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // toggled buttons
  ["blockquote", "code-block"],

  [
    {
      header: 1,
    },
    {
      header: 2,
    },
  ], // custom button values
  [
    {
      list: "ordered",
    },
    {
      list: "bullet",
    },
  ],
  [
    {
      script: "sub",
    },
    {
      script: "super",
    },
  ], // superscript/subscript
  [
    {
      indent: "-1",
    },
    {
      indent: "+1",
    },
  ], // outdent/indent
  [
    {
      direction: "rtl",
    },
  ], // text direction

  [
    {
      size: ["small", false, "large", "huge"],
    },
  ], // custom dropdown
  [
    {
      header: [1, 2, 3, 4, 5, 6, false],
    },
  ],

  [
    {
      color: [],
    },
    {
      background: [],
    },
  ], // dropdown with defaults from theme
  [
    {
      font: [],
    },
  ],
  [
    {
      align: [],
    },
  ],
  ["link", "image", "video"],
  ["clean"], // remove formatting button
];

export default {
  components: {
    SingleUpload,
  },
  data() {
    return {
      // 上传图片接口地址
      imgUploadUrl: "http://localhost:8150/uploadImgFile",

      // 富文本编辑器工具栏
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              image: function (value) {
                if (value) {
                  // 调用element的图片上传组件
                  // （这里是用的原生js的选择dom方法）
                  document.querySelector("#upimg button").click();
                } else {
                  this.quill.format("image", false);
                }
              },
            },
          },
        },
      },

      visible: false,
      dataForm: {
        id: 0,
        title: "",
        images: "",
        descs: "",
        activeTime: "",
        deleteTime: "",
        firstPrize: "",
        secondPrize: "",
        thiredPrize: "",
        firstPeople: "",
        secondPeople: "",
        thirdPoeple: "",
        participationAward: "",
        participationPeople: "",

        status: "",
      },
      dataRule: {
        title: [
          { required: true, message: "活动名称不能为空", trigger: "blur" },
        ],
        images: [
          { required: true, message: "活动图片不能为空", trigger: "blur" },
        ],
        descs: [{ required: true, message: "描述不能为空", trigger: "blur" }],
        // activeTime: [
        //   { required: true, message: '活动开始时间不能为空', trigger: 'blur' }
        // ],
        // deleteTime: [
        //   { required: true, message: '活动结束时间不能为空', trigger: 'blur' }
        // ],
        firstPrize: [
          { required: true, message: "一等奖不能为空", trigger: "blur" },
        ],
        secondPrize: [
          { required: true, message: "二等奖不能为空", trigger: "blur" },
        ],
        thiredPrize: [
          { required: true, message: "三等奖不能为空", trigger: "blur" },
        ],
        firstPeople: [
          { required: true, message: "一等奖人数不能为空", trigger: "blur" },
        ],
        secondPeople: [
          { required: true, message: "二等奖人数不能为空", trigger: "blur" },
        ],
        thirdPoeple: [
          { required: true, message: "三等奖人数不能为空", trigger: "blur" },
        ],
        participationAward: [
          { required: true, message: "参与奖不能为空", trigger: "blur" },
        ],
        participationPeople: [
          { required: true, message: "参与奖个数不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
        createDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateDate: [
          { required: true, message: "更新时间不能为空", trigger: "blur" },
        ],
        isDelete: [
          { required: true, message: "状态不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // element的upload组件上传图片成功后调用的函数
    handleSuccess(res) {
      // 获取富文本组件实例
      console.log(res);
      let quill = this.$refs.QuillEditor.quill;
      if (res.code == 0) {
        // 如果上传成功
        // 获取光标所在位置
        let length = quill.getSelection().index;
        // 插入图片，res为服务器返回的图片链接地址
        quill.insertEmbed(length, "image", res.url);
        // 调整光标到最后
        quill.setSelection(length + 1);
      } else {
        // 提示信息
        this.$message.error("图片插入失败");
      }
    },

    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/activity/activity/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.title = data.activity.title;
              this.dataForm.images = data.activity.images;
              this.dataForm.descs = data.activity.descs;
              this.dataForm.activeTime = data.activity.activeTime;
              this.dataForm.deleteTime = data.activity.deleteTime;
              this.dataForm.firstPrize = data.activity.firstPrize;
              this.dataForm.secondPrize = data.activity.secondPrize;
              this.dataForm.thiredPrize = data.activity.thiredPrize;
              this.dataForm.firstPeople = data.activity.firstPeople;
              this.dataForm.secondPeople = data.activity.secondPeople;
              this.dataForm.thirdPoeple = data.activity.thirdPoeple;
              this.dataForm.participationAward =
                data.activity.participationAward;
              this.dataForm.participationPeople =
                data.activity.participationPeople;
              this.dataForm.createBy = data.activity.createBy;
              this.dataForm.updateBy = data.activity.updateBy;
              this.dataForm.createDate = data.activity.createDate;
              this.dataForm.updateDate = data.activity.updateDate;
              this.dataForm.status = data.activity.status;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/activity/activity/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              title: this.dataForm.title,
              images: this.dataForm.images,
              descs: this.dataForm.descs,
              activeTime: this.dataForm.activeTime,
              deleteTime: this.dataForm.deleteTime,
              firstPrize: this.dataForm.firstPrize,
              secondPrize: this.dataForm.secondPrize,
              thiredPrize: this.dataForm.thiredPrize,
              firstPeople: this.dataForm.firstPeople,
              secondPeople: this.dataForm.secondPeople,
              thirdPoeple: this.dataForm.thirdPoeple,
              participationAward: this.dataForm.participationAward,
              participationPeople: this.dataForm.participationPeople,
              createBy: this.dataForm.createBy,
              updateBy: this.dataForm.updateBy,
              createDate: this.dataForm.createDate,
              updateDate: this.dataForm.updateDate,
              status: this.dataForm.status,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
